package Common;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class WebDriver {
    public static ChromeDriver driver;

    public static ChromeDriver getDriver(){
        if (driver == null){
            driver = new ChromeDriver();

            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }
        return driver;
    }


    public static List<String> getName(){
        //文件名称
        SimpleDateFormat str1 = new SimpleDateFormat("yyyyMMdd-HHmmssSS");
        //文件夹名称
        SimpleDateFormat str2 = new SimpleDateFormat("yyyy-MM-dd");
        String dirName = str2.format(System.currentTimeMillis());
        String fileName = str1.format(System.currentTimeMillis());

        List<String> list = new ArrayList<String>();
        list.add(dirName);
        list.add(fileName);
        return list;
    }


    //获取屏幕截图
    public static void getPho(String s) throws IOException{
        List<String> list = getName();
        //文件路径
        String fileName = "./src/test-"+s+"-"+list.get(0)+list.get(1)+".png";
        File file = driver.getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File(fileName));
    }
}

